#include "epbc.h"

void fn_pbc_new(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	struct pbc_env *env = pbc_new();
	pRetData->m_int = (INT)env;
}

void fn_pbc_delete(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
		pbc_delete((pbc_env*)pArgInf->m_int);
}

void fn_pbc_register(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_env *env = (pbc_env*)pArgInf->m_int;
		struct pbc_slice slice;
		slice.buffer = GetBinPtr(&pArgInf[1]);
		slice.len = GetDataSize(&pArgInf[1]);
		pRetData->m_int = pbc_register(env, &slice);
	}
}

void fn_pbc_type(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_env *env = (pbc_env*)pArgInf->m_int;
		const char* szType = NULL;
		pRetData->m_int = pbc_type(env, pArgInf[1].m_pText, pArgInf[2].m_pText, &szType);
		MessageBoxA(NULL, szType, NULL, MB_OK);
		if (szType != NULL)
		{
			*(pArgInf + 3)->m_ppText = CloneTextData((char*)szType, strlen(szType));
		}
	}
}

void fn_pbc_error(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_env *env = (pbc_env*)pArgInf->m_int;
		const char* szError = pbc_error(env);
		if (szError != NULL)
		{
			pRetData->m_pText = CloneTextData((char*)szError, strlen(szError));
		}
	}
}

void fn_pbc_wmessage_new(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_env *env = (pbc_env*)pArgInf->m_int;
		struct pbc_wmessage *w_msg = pbc_wmessage_new(env, pArgInf[1].m_pText);
		pRetData->m_int = (INT)w_msg;
	}
}

void fn_pbc_wmessage_delete(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
		pbc_wmessage_delete((pbc_wmessage*)pArgInf->m_int);
}

void fn_pbc_wmessage_integer(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_wmessage *w_msg = (pbc_wmessage*)pArgInf->m_int;
		pRetData->m_int = pbc_wmessage_integer(w_msg, pArgInf[1].m_pText, pArgInf[2].m_int, pArgInf[3].m_int);
	}
}

void fn_pbc_wmessage_real(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_wmessage *w_msg = (pbc_wmessage*)pArgInf->m_int;
		pRetData->m_int = pbc_wmessage_real(w_msg, pArgInf[1].m_pText, pArgInf[2].m_double);
	}
}

void fn_pbc_wmessage_string(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_wmessage *w_msg = (pbc_wmessage*)pArgInf->m_int;
		pRetData->m_int = pbc_wmessage_string(w_msg, pArgInf[1].m_pText, pArgInf[2].m_pText, GetDataSize(&pArgInf[2]));
	}
}

void fn_pbc_wmessage_buffer(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_wmessage *w_msg = (pbc_wmessage*)pArgInf->m_int;	
		LPBYTE lpData =  GetBinPtr(&pArgInf[2]);
		DWORD lpSize = GetDataSize(&pArgInf[2]);
		if (lpData == NULL || lpSize == 0)
		{
			pRetData->m_int = pbc_wmessage_string(w_msg, pArgInf[1].m_pText, "", 0);
		}
		else
		{
			pRetData->m_int = pbc_wmessage_string(w_msg, pArgInf[1].m_pText, (char*)lpData, lpSize);
		}	
	}
}

void fn_pbc_wmessage_message(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_wmessage *w_msg = (pbc_wmessage*)pArgInf->m_int;
		pRetData->m_int = (INT)pbc_wmessage_message(w_msg, pArgInf[1].m_pText);
	}
}

void fn_pbc_wmessage_to_buffer(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_wmessage *w_msg = (pbc_wmessage*)pArgInf->m_int;
		struct pbc_slice slice;
		pbc_wmessage_buffer(w_msg, &slice);
		pRetData->m_pBin = CloneBinData((LPBYTE)slice.buffer, slice.len);
	}
}

void fn_pbc_rmessage_new(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_env *env = (pbc_env*)pArgInf->m_int;

		struct pbc_slice slice;
		slice.buffer = GetBinPtr(&pArgInf[2]);
		slice.len = GetDataSize(&pArgInf[2]);

		struct pbc_rmessage *r_msg = pbc_rmessage_new(env, pArgInf[1].m_pText, &slice);

		pRetData->m_int = (INT)r_msg;
	}
}

void fn_pbc_rmessage_delete(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
		pbc_rmessage_delete((pbc_rmessage*)pArgInf->m_int);
}

void fn_pbc_rmessage_integer(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_rmessage *r_msg = (pbc_rmessage*)pArgInf->m_int;	
		uint32_t hi;
		pRetData->m_int = pbc_rmessage_integer(r_msg, pArgInf[1].m_pText, pArgInf[2].m_int, &hi);
		if (pArgInf[3].m_dtDataType != _SDT_NULL)
			*(pArgInf + 3)->m_pInt = hi;		
	}
}

void fn_pbc_rmessage_real(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_rmessage *r_msg = (pbc_rmessage*)pArgInf->m_int;
		pRetData->m_double = pbc_rmessage_real(r_msg, pArgInf[1].m_pText, pArgInf[2].m_int);
	}
}

void fn_pbc_rmessage_string(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_rmessage *r_msg = (pbc_rmessage*)pArgInf->m_int;
		int sz = 0;
		const char* szStr = pbc_rmessage_string(r_msg, pArgInf[1].m_pText, pArgInf[2].m_int, &sz);
		if (szStr && sz)
			pRetData->m_pText = CloneTextData((char*)szStr, sz);
	}
}

void fn_pbc_rmessage_buffer(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_rmessage *r_msg = (pbc_rmessage*)pArgInf->m_int;
		int sz = 0;
		const char* szStr = pbc_rmessage_string(r_msg, pArgInf[1].m_pText, pArgInf[2].m_int, &sz);
		if (szStr && sz)
			pRetData->m_pBin = CloneBinData((LPBYTE)szStr, sz);
	}
}

void fn_pbc_rmessage_message(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_rmessage *r_msg = (pbc_rmessage*)pArgInf->m_int;
		pRetData->m_int = (INT)pbc_rmessage_message(r_msg, pArgInf[1].m_pText, pArgInf[2].m_int);
	}
}

void fn_pbc_rmessage_size(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_rmessage *r_msg = (pbc_rmessage*)pArgInf->m_int;
		pRetData->m_int = pbc_rmessage_size(r_msg, pArgInf[1].m_pText);
	}
}

void fn_pbc_rmessage_next(PMDATA_INF pRetData, INT nArgCount, PMDATA_INF pArgInf)
{
	if (pArgInf->m_int != 0)
	{
		struct pbc_rmessage *r_msg = (pbc_rmessage*)pArgInf->m_int;
		const char *szKey = NULL;
		pRetData->m_int = pbc_rmessage_next(r_msg, &szKey);
		if (szKey != NULL)
			*(pArgInf + 1)->m_ppText = CloneTextData((char*)szKey, strlen(szKey));
	}
}



